package com.tim.android.widget.listview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;

public class GroupableListView extends FrameLayout implements OnScrollListener {
	
	private static final String TAG = "GroupableListView";
	
	private ExpandableListView mListView;
	private GroupView mGroupLayout;
	
	private Context mContext;
	private GroupableListAdapter mAdapter;
	
	private int mCurrentGroupPosition = -1;

	public GroupableListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		initComponent();
	}

	public GroupableListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		initComponent();
	}

	public GroupableListView(Context context) {
		super(context);
		this.mContext = context;
		setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		initComponent();
	}
	
	private void initComponent() {
		mListView = new ExpandableListView(mContext);
		mGroupLayout = new GroupView(mContext);

		mListView.setCacheColorHint(Color.TRANSPARENT);
		mListView.setGroupIndicator(null);
		mListView.setChildIndicator(null);
		mListView.setOnScrollListener(this);
		mGroupLayout.setVisibility(View.GONE);
		
		addView(mListView);
		addView(mGroupLayout);
	}
	
	public void setAdapter(GroupableListAdapter adapter) {
		if(adapter != null) {
			this.mAdapter = adapter;
			mListView.setAdapter(adapter);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Log.e(TAG, "onScroll >> ");
		final int pointToPosition = view.pointToPosition(0, 0);
		Log.e(TAG, "pointToPosition >> " + pointToPosition);
		if(pointToPosition != AdapterView.INVALID_POSITION) {
			final long pos = mListView.getExpandableListPosition(pointToPosition);
			final int childPos = ExpandableListView.getPackedPositionChild(pos);
			int groupPos = ExpandableListView.getPackedPositionGroup(pos);
			
			Log.e(TAG, "pos = " + pos + ", groupPos = " + groupPos + " , childPos = " + childPos);
			
			if(childPos < 0) {
				groupPos = -1;
			}
			if(groupPos < mCurrentGroupPosition) {
				mCurrentGroupPosition = groupPos;
				mGroupLayout.setVisibility(View.GONE);
			}
			else if(groupPos > mCurrentGroupPosition) {
				final String groupName = mAdapter.getGroupItem(groupPos).getGroupName();
				mCurrentGroupPosition = groupPos;
				mGroupLayout.setVisibility(View.VISIBLE);
				mGroupLayout.groupTitle.setText(groupName);
			}
		}
	}

//	@Override
//	public void onScroll(AbsListView view, int firstVisibleItem,
//			int visibleItemCount, int totalItemCount) {
//		Log.e(TAG, "onScroll ...");
//		final int groupPosition = firstVisibleItem / 10;
//		final int firstVisibleItemIndex = firstVisibleItem % 10;
//		if(mAdapter == null || groupPosition < 0 ||
//				groupPosition > mAdapter.getGroupCount()) {
//			return ;
//		}
//		final int childCount = mAdapter.getChildrenCount(groupPosition);
//		if(firstVisibleItemIndex < groupPosition || firstVisibleItemIndex > (groupPosition + childCount)) {
//			mGroupLayout.setVisibility(View.GONE);
//		}
//		else {
//			final String groupName = mAdapter.getGroupItem(groupPosition).getGroupName();
//			mGroupLayout.setVisibility(View.VISIBLE);
//			mGroupLayout.groupTitle.setText(groupName);
//		}
	
//	}
}
